package admin_sales;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.Properties;

import javax.mail.*;
import javax.mail.internet.*;

import java.io.IOException;

import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Part;

import java.util.concurrent.TimeUnit;

import org.testng.*;
import org.testng.annotations.*;

import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;

import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.Select;

import admin_sales.Orders_email.GetMulti;

public class Orders_email extends ru.st.selenium.pages.TestBase {
  private boolean acceptNextAlert = true;
  private StringBuffer verificationErrors = new StringBuffer();

    @Test
  public void testUntitled6() throws Exception {
    driver.manage().window().maximize();
    Actions actions = new Actions(driver);
    
    	class MailAuthenticator extends Authenticator {
   		 
    	    public PasswordAuthentication getPasswordAuthentication() {
    	         return new PasswordAuthentication("qatestingtestqa@gmail.com", "parol123");
    	    }
    	}
    	
    	Properties props = new Properties();
    	
    	props.put("mail.smtp.user", "qatestingtestqa@gmail.com");
    	props.put("mail.host", "pop.gmail.com");
    	props.put("mail.store.protocol", "pop3s");
        props.put("mail.pop3s.auth", "true");
        props.put("mail.pop3s.port", 995);
    	props.put("mail.smtp.starttls.enable","true");
    	props.put("mail.smtp.debug", "true");
    	props.put("mail.smtp.auth", "true");
    	props.put("mail.smtp.socketFactory.port", 995);
    	props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
    	props.put("mail.smtp.socketFactory.fallback", "false");

    	Session session = Session.getInstance(props, new MailAuthenticator());
    	session.setDebug(true);
	       	  
    	

    	
	        Store store = session.getStore("pop3s");
	        store.connect("pop.gmail.com", "qatestingtestqa@gmail.com", "parol123");
	 
	        Folder inbox = store.getFolder("INBOX");
	        inbox.open(Folder.READ_ONLY);
	        
	        Message[] messages = inbox.getMessages();
	        
	           
	        GetMulti gmulti = new GetMulti();
	        String textMessage = gmulti.getText(messages[messages.length - 1]);
	        String regex = "Thank you for your order from Main Store. Once your package ships we will send an email with a link to track your order. You can check the status of your order by logging into your account. If you have any questions about your order please contact us at support@example.com or call us at Monday - Friday, 8am - 5pm PST.";
	        Pattern p = Pattern.compile(regex);
	        Matcher m = p.matcher(textMessage);
	        if (m.find()) {
	        	    driver.get(baseUrl);
	        	    driver.findElement(By.id("search")).clear();
	        	    driver.findElement(By.id("search")).sendKeys(m.group());
	        	TimeUnit.SECONDS.sleep(5);
	        	   
	        }
	        inbox.close(false);
	        store.close(); 
	        driver.get(baseUrl);
	     //   driver.quit();  
  }

   private boolean isElementPresent(By by) {
    try {
      driver.findElement(by);
      return true;
    } catch (NoSuchElementException e) {
      return false;
    }
  }

  
  private String closeAlertAndGetItsText() {
    try {
      Alert alert = driver.switchTo().alert();
      String alertText = alert.getText();
      if (acceptNextAlert) {
        alert.accept();
      } else {
        alert.dismiss();
      }
      return alertText;
    } finally {
      acceptNextAlert = true;
    }
  }
  public class GetMulti {
	    
	    public  boolean textIsHtml = false;
	 
	    /**
	     * Return the primary text content of the message.
	     */
	    public String getText(Part p) throws MessagingException, IOException {
	        if (p.isMimeType("text/*")) {
	            String s = (String)p.getContent();
	            textIsHtml = p.isMimeType("text/html");
	            return s;
	        }
	 
	        if (p.isMimeType("multipart/alternative")) {
	            // prefer html text over plain text
	            Multipart mp = (Multipart)p.getContent();
	            String text = null;
	            for (int i = 0; i < mp.getCount(); i++) {
	                Part bp = mp.getBodyPart(i);
	                if (bp.isMimeType("text/plain")) {
	                    if (text == null)
	                        text = getText(bp);
	                    continue;
	                } else if (bp.isMimeType("text/html")) {
	                    String s = getText(bp);
	                    if (s != null)
	                        return s;
	                } else {
	                    return getText(bp);
	                }
	            }
	            return text;
	        } else if (p.isMimeType("multipart/*")) {
	            Multipart mp = (Multipart)p.getContent();
	            for (int i = 0; i < mp.getCount(); i++) {
	                String s = getText(mp.getBodyPart(i));
	                if (s != null)
	                    return s;
	            }
	        }
	 
	        return null;
	    }
	 
	}
}
